package com.qjdchina.sku.biz.impl;

import com.qjdchina.commons.enums.IsDeleted;
import com.qjdchina.sku.dao.mapper.CategoryBrandAssociateMapper;
import com.qjdchina.sku.model.CategoryBrandAssociate;
import com.qjdchina.sku.model.vo.CategoryBrandAssoVO;
import com.qjdchina.sku.model.vo.CategoryBrandRegionVO;
import com.qjdchina.sku.model.vo.CategoryBrandVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.CategoryBrandManager;
import com.qjdchina.sku.biz.CategoryManager;
import com.qjdchina.sku.dao.mapper.CategoryBrandMapper;
import com.qjdchina.sku.model.CategoryBrand;
import com.qjdchina.sku.service.enums.SkuResultCode;

import java.util.Date;
import java.util.List;

@Service
public class CategoryBrandManagerImpl implements CategoryBrandManager {

	private static final Logger LOGGER = LoggerFactory.getLogger(CategoryManager.class);

	@Autowired
	private CategoryBrandMapper categoryBrandMapper;

	@Autowired
	private CategoryBrandAssociateMapper categoryBrandAssociateMapper;

	/**
	 * 根据skuBrandId查询 shenpingfeng 2017年1月11日
	 * 
	 * @param skuBrandId
	 * @return
	 */
	@Override
	public Result<CategoryBrand> getCategoryBrandBySkuBrandIdAndSkuCategoryId(Integer skuBrandId,
			Integer skuCategoryId) {
		if (skuBrandId == null || skuCategoryId == null) {
			LOGGER.warn("skuBrandId {}",skuBrandId,"skuCategoryId {}",skuCategoryId);
			return new Result<CategoryBrand>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		CategoryBrand categoryBrand = categoryBrandMapper.selectBySkuBrandIdAndSkuCategoryId(skuBrandId, skuCategoryId);
		if (categoryBrand == null) {
			return new Result<CategoryBrand>(SkuResultCode.DATA_NOT_EXIST);
		}
		return new Result<CategoryBrand>(categoryBrand);
	}

	/**
	 * 更新 shenpingfeng 2017年2月17日
	 * 
	 * @param categoryBrandVO
	 * @return
	 */
	@Override
	public Result<String> update(CategoryBrandVO categoryBrandVO) {
		if (categoryBrandVO == null || categoryBrandVO.getSkuCategoryBrandId() == null) {
			return new Result<String>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		CategoryBrand categoryBrand = new CategoryBrand();
		categoryBrand.setId(categoryBrandVO.getSkuCategoryBrandId());
		categoryBrand.setGmtModified(new Date());
		categoryBrand.setSupplyScope(categoryBrandVO.getSupplyScope());
		categoryBrand.setFreightDifference(categoryBrandVO.getFreightDifference());
		int row = categoryBrandMapper.updateByPrimaryKeySelective(categoryBrand);
		if (row == 0) {
			return new Result<String>(SkuResultCode.UPDATE_FAIL);
		}

		saveCategoryBrandItem(categoryBrandVO.getSkuCategoryBrandId(), categoryBrandVO.getCategoryBrandAssoVOList());
		saveCategoryBrandRegion(categoryBrandVO.getSkuCategoryBrandId(), categoryBrandVO.getCategoryBrandRegionVOList());

		return new Result<String>(row + "");
	}

	@Override
	public Result<String> delete(CategoryBrand categoryBrand){
		categoryBrand.setIsDeleted(IsDeleted.YES.getValue());
		int row = categoryBrandMapper.updateByPrimaryKeySelective(categoryBrand);
		if (row == 0) {
			return new Result<String>(SkuResultCode.UPDATE_FAIL);
		}
		return new Result<String>(row + "");
	}

	private void saveCategoryBrandItem(Integer skuCategoryBrandId, List<CategoryBrandAssoVO> categoryBrandAssoVOList) {
		if (categoryBrandAssoVOList != null && categoryBrandAssoVOList.size() > 0) {
			for (CategoryBrandAssoVO categoryBrandAssoVO : categoryBrandAssoVOList) {
				CategoryBrandAssociate categoryBrandAssociate = new CategoryBrandAssociate();
				if (categoryBrandAssoVO.getId() == null || categoryBrandAssoVO.getId() == 0) {
					categoryBrandAssociate.setSkuCategoryBrandId(skuCategoryBrandId);
					categoryBrandAssociate.setSkuItemId(categoryBrandAssoVO.getSkuItemId());
					categoryBrandAssociate.setAttrValue(categoryBrandAssoVO.getAttrValue());
					categoryBrandAssociate.setIsDeleted("0");
					categoryBrandAssociate.setGmtCreated(new Date());
					categoryBrandAssociate.setGmtModified(new Date());
					categoryBrandAssociateMapper.insertSelective(categoryBrandAssociate);
				} else {
					categoryBrandAssociate.setId(categoryBrandAssoVO.getId());
					categoryBrandAssociate.setAttrValue(categoryBrandAssoVO.getAttrValue());
					categoryBrandAssociate.setGmtModified(new Date());
					categoryBrandAssociateMapper.updateByPrimaryKeySelective(categoryBrandAssociate);
				}
			}
		}
	}

	private void saveCategoryBrandRegion(Integer skuCategoryBrandId, List<CategoryBrandRegionVO> categoryBrandRegionVOList) {
		if (categoryBrandRegionVOList != null && categoryBrandRegionVOList.size() > 0) {
			for (CategoryBrandRegionVO categoryBrandRegionVO : categoryBrandRegionVOList) {
				CategoryBrandAssociate categoryBrandAssociate = new CategoryBrandAssociate();
				if (categoryBrandRegionVO.getId() == null || categoryBrandRegionVO.getId() == 0) {
					categoryBrandAssociate.setSkuCategoryBrandId(skuCategoryBrandId);
					categoryBrandAssociate.setSkuRegionId(categoryBrandRegionVO.getRegionId());
					categoryBrandAssociate.setAttrValue(categoryBrandRegionVO.getAttrValue());
					categoryBrandAssociate.setIsDeleted("0");
					categoryBrandAssociate.setGmtCreated(new Date());
					categoryBrandAssociate.setGmtModified(new Date());
					categoryBrandAssociateMapper.insertSelective(categoryBrandAssociate);
				} else {
					categoryBrandAssociate.setId(categoryBrandRegionVO.getId());
					categoryBrandAssociate.setAttrValue(categoryBrandRegionVO.getAttrValue());
					categoryBrandAssociate.setGmtModified(new Date());
					categoryBrandAssociateMapper.updateByPrimaryKeySelective(categoryBrandAssociate);
				}
			}
		}
	}
}
